/*
 * Copyright (C) 2012 Vitaljok
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/*
 * ChromosomeTestFrame.java
 *
 * Created on Aug 19, 2012, 11:52:00 AM
 */
package phd.mrs.heuristic;

import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableRowSorter;
import org.jgap.IChromosome;
import phd.mrs.heuristic.object.Project;
import phd.mrs.heuristic.ga.AgentGene;
import phd.mrs.heuristic.mission.Mission;
import phd.mrs.heuristic.object.Agent;
import phd.mrs.heuristic.object.Component;

/**
 *
 * @author Vitaljok
 */
public class ChromosomeTestFrame extends javax.swing.JFrame {

    IChromosome chromosome;
    Project project;

    public ChromosomeTestFrame(final IChromosome chromosome, final Project project) {
        this.chromosome = chromosome;
        this.project = project;
        initComponents();

        MyTableModel model = new MyTableModel(chromosome);
        this.jTable.setModel(model);

        this.jTable.setRowSorter(new TableRowSorter<MyTableModel>(model));

        this.jTable.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_LAST_COLUMN);
        this.jTable.getColumnModel().getColumn(0).setMaxWidth(50);
        this.jTable.getColumnModel().getColumn(1).setMaxWidth(50);

        this.jTable.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
            @Override
            public void valueChanged(ListSelectionEvent e) {

                if (jTable.getSelectedRow() >= 0) {
                    Integer rowIndex = jTable.convertRowIndexToModel(jTable.getSelectedRow());
                    Agent agent = ((AgentGene) chromosome.getGenes()[rowIndex]).getAgent();
                    String txt = "Components: \n";
                    for (Component comp : agent.getComponents()) {
                        txt += "\t" + comp + "\n";
                    }

                    txt += "\nMission performace:\n";

                    for (Mission mis : project.getMissions()) {
                        txt += mis.getClass().getSimpleName() + "\n\t" + mis.getAgentPerformance(agent) + "\n";
                    }



                    jTextArea.setText(txt);
                }

            }
        });



    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jTable = new javax.swing.JTable();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTextArea = new javax.swing.JTextArea();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jScrollPane1.setViewportView(jTable);

        jTextArea.setColumns(20);
        jTextArea.setRows(5);
        jScrollPane2.setViewportView(jTextArea);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 704, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 315, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 656, Short.MAX_VALUE)
                    .addComponent(jScrollPane2))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTable jTable;
    private javax.swing.JTextArea jTextArea;
    // End of variables declaration//GEN-END:variables

    class MyTableModel extends AbstractTableModel {

        String[] colNames = {"Inst.", "Comps", "Agent"};
        IChromosome chrom;

        public MyTableModel(IChromosome chrom) {
            this.chrom = chrom;
        }

        @Override
        public boolean isCellEditable(int rowIndex, int columnIndex) {
            return columnIndex == 0;
        }

        @Override
        public int getRowCount() {
            return chrom.size();
        }

        @Override
        public int getColumnCount() {
            return 3;
        }

        @Override
        public Class<?> getColumnClass(int columnIndex) {
            return getValueAt(0, columnIndex).getClass();
        }

        @Override
        public String getColumnName(int column) {
            return colNames[column];
        }

        @Override
        public Object getValueAt(int rowIndex, int columnIndex) {
            switch (columnIndex) {
                case 0:
                    return (Integer) this.chrom.getGenes()[rowIndex].getAllele();
                case 1:
                    return ((AgentGene) this.chrom.getGenes()[rowIndex]).getAgent().getComponents().size();
                case 2:
                    return ((AgentGene) this.chrom.getGenes()[rowIndex]).getAgent().getComponents();
                default:
                    throw new AssertionError();
            }
        }

        @Override
        public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
            if (aValue == null) {
                aValue = new Integer(0);
            }
            super.setValueAt(aValue, rowIndex, columnIndex);
            this.chrom.getGenes()[rowIndex].setAllele(aValue);
        }
    }
}
